/*
  #include <unistd.h>

       extern char **environ;

       int execl(const char *path, const char *arg, ...);
       int execlp(const char *file, const char *arg, ...);
       int execle(const char *path, const char *arg,
                  ..., char * const envp[]);
       int execv(const char *path, char *const argv[]);
       int execvp(const char *file, char *const argv[]);
       int execvpe(const char *file, char *const argv[],
                   char *const envp[]);

*/
#include <stdio.h>
#include <stdlib.h> /* int setenv(char *name, char *value, int overwrite) */
#include <unistd.h>
#include <sys/types.h>
#include "error.h"

int main(int argc, char * argv[]) {
	pid_t pid;	
	
	if((pid = vfork()) < 0) {
		err_sys("Vfork error.");
	} else if(pid == 0) {
		fprintf(stdout, "Child process PID: %d.\n", getpid());
		setenv("PS1", "CHILD\\$", 1);
		fprintf(stdout, "Process%4d: calling exec.\n", getpid());
		if(execl("/bin/sh", "/bin/sh", "arg2", NULL) < 0) {
			printf("Process%4d: execl error.\n", getpid());
			exit(0);
		}
		fprintf(stdout, "Process%4d: You should never see this because the child is already gnone.\n", getpid());
		fprintf(stdout, "Process%4d: The child process is exiting.\n", getpid());
	} else {
		fprintf(stdout, "Parent process PID:%4d\n", getpid());
		fprintf(stdout, "Process%4d: The parent has fork process %4d.\n", pid, pid);
		fprintf(stdout, "Process%4d: The child has called exec or has exit.\n", getpid());
	}

	return 0;
}
